//
//  FLLClassifyTableViewCell.m
//  FLLGodOfCooking
//
//  Created by LL.F on 16/9/9.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLClassifyTableViewCell.h"
#import "Masonry.h"
#import "FLLMacro.h"
#import "FLLClassifyCollectionViewCell.h"
#import "UIImageView+WebCache.h"

@interface FLLClassifyTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, waterCollectionFlowLayout>
@end

@implementation FLLClassifyTableViewCell

- (void)awakeFromNib {
    // Initialization code
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUp];
    }
    return self;
}
- (void)setUp
{
    
    self.grayLabel = [[UILabel alloc] init];
    self.grayLabel.backgroundColor = FLLLightGrayColor;
    [self.contentView addSubview:_grayLabel];
    
    [self createMaterialCollectionView];
    
}
- (void)createMaterialCollectionView
{
    
    self.classifyLayout = [[FLLMaterialViewFlowLayout alloc] init];
    _classifyLayout.delegate = self;
    
    
    self.classifyCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_classifyLayout];
    [self.contentView addSubview:_classifyCollectionView];
    self.classifyCollectionView.delegate = self;
    self.classifyCollectionView.dataSource = self;
    _classifyCollectionView.backgroundColor = [UIColor whiteColor];
    [_classifyCollectionView registerNib:[UINib nibWithNibName:@"FLLClassifyCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"classifyCollectionCell"];
    
}
- (void)layoutSubviews
{
    
    [super layoutSubviews];
    
    [_grayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(10);
    }];
    
    [_classifyCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.bottom.mas_equalTo(_grayLabel.mas_top);
    }];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    return _itemArray.count ;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{

    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    FLLClassifyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"classifyCollectionCell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = FLLLightGrayColor;
    cell.dishNameLabel.textColor = [UIColor grayColor];
    cell.dishNameLabel.font = [UIFont systemFontOfSize:15 * kWidthScale];
    if (indexPath.row == 0) {
        cell.dishNameLabel.text = nil;
        [cell.dishImageView sd_setImageWithURL:[NSURL URLWithString:_itemArray[indexPath.row].image]placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
        }];
    } else {
    
        cell.dishNameLabel.text = _itemArray[indexPath.row].text;
        cell.dishImageView.image = [UIImage new];
    }
       return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGFloat itemWidth = (_classifyCollectionView.bounds.size.width - 6 * CellMargin_10) / 5.0;
    CGFloat itemHeight = itemWidth / 1.5;
    if (indexPath.row == 0) {
        return CGSizeMake(2 * itemWidth + CellMargin_10, 2 * itemHeight + CellMargin_10);
    }
    
    return CGSizeMake(itemWidth, itemHeight);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row != 0) {
        // 传出 id 和 title
        self.pushBlock(_itemArray[indexPath.row].id_Data, _itemArray[indexPath.row].text);
        
    }
    
}
#warning mark -- 没有效果
// 改变行 列间距 没有效果怎么破???????????
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return CellMargin_10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return CellMargin_10;
}
- (void)setItemArray:(NSArray<ClassifyThirdData *> *)itemArray
{

    _itemArray = itemArray;
    [self.classifyCollectionView reloadData];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
